﻿//Copyright (c) 2009, Offbeat Guides
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//* Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other materials 
//provided with the distribution.
//
//* Neither the name of Offbeat Guides nor the names of its contributors may be used to 
//endorse or promote products derived from this software without specific prior written 
//permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Collections.Generic;
using System.Text;
using Amazon.Services;
using System.Xml.XPath;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
using System.Net;

namespace Amazon.Services.EC2
{
    [Serializable]
    [XmlRoot("item", Namespace = "http://ec2.amazonaws.com/doc/2009-04-04/")]
    public partial class Instance : System.Xml.Serialization.IXmlSerializable
    {
        #region Static Variables
        private static Instance CurrentInstance;
        #endregion

        #region Variables
        private Amazon.Services.ServiceClient _client;
        #endregion

        #region Properties
        public String Id { get; private set; }
        
        public String Type { get; private set; }
        
        public String ImageId { get; private set; }
        
        public String KeyName { get; private set; }
        
        public DateTime LauchTime { get; private set; }
        
        public String Platform { get; private set; }
        
        public String PrivateDnsName { get; private set; }
        
        public String PublicDnsName { get; private set; }

        public InstanceStates State { get; private set; }
        #endregion

        #region Constructors and Finalizers
        internal Instance(ServiceClient client)
        {
            this._client = client;
        }
        #endregion

        #region Methods
        public void Destroy()
        {
            Instance.Destroy(this._client.Account, this.Id);
        }

        public Instance Duplicate()
        {
            return Instance.Create(this._client.Account, this.ImageId);
        }
        #endregion

        #region IXmlSerializable Methods
        XmlSchema IXmlSerializable.GetSchema()
        {
            return null;
        }

        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (!reader.Name.Equals("item", StringComparison.OrdinalIgnoreCase))
                reader.ReadToFollowing("item");

            reader.ReadStartElement();
            {
                while (!reader.Name.Equals("item", StringComparison.OrdinalIgnoreCase) || reader.NodeType != XmlNodeType.EndElement)
                {
                    switch (reader.Name.ToLower())
                    {
                        case "instanceid":
                            this.Id = reader.ReadElementString();
                            break;
                        case "imageid":
                            this.ImageId = reader.ReadElementString();
                            break;
                        case "privatednsname":
                            this.PrivateDnsName = reader.ReadElementString();
                            break;
                        case "dnsname":
                            this.PublicDnsName = reader.ReadElementString();
                            break;
                        case "instancestate":
                            reader.ReadStartElement();
                            {
                                this.State = (InstanceStates)Convert.ToInt32(reader.ReadElementString());
                                reader.ReadElementString();
                            }
                            reader.ReadEndElement();
                            break;
                        case "launchtime":
                            this.LauchTime = DateTime.Parse(reader.ReadElementString());
                            break;
                        case "keyname":
                            this.KeyName = reader.ReadElementString();
                            break;
                        default:
                            reader.ReadOuterXml();
                            break;
                    }
                }
            }
            reader.ReadEndElement();
        }

        void IXmlSerializable.WriteXml(XmlWriter writer)
        {

        }
        #endregion

        #region Overriden Methods
        public override bool Equals(object obj)
        {
            Instance instance = obj as Instance;
            return (instance != null && instance.Id.Equals(this.Id, StringComparison.OrdinalIgnoreCase));
        }
        #endregion
    }
}
